package com.example.commons.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * java8日期新的管理类
 */
public class LocalDateTimeUtil {
    /**
     * 计算俩个时间的相差
     * 考试结束时间 -剩余时间
     * @return
     */
    public static  String timeBetween(LocalDateTime time1,LocalDateTime time2){

        long diff1 = ChronoUnit.SECONDS.between(time1, time2);

        long h=diff1/3600*-1;			//小时
        long f=(diff1%3600)/60*-1;		//分钟
        long s=(diff1%3600)%60*-1;		//秒
        return h+"小时"+f+"分钟"+s+"秒";
    }

    /**
     * 计算是否能进入考试，超过考试时间就不能进入
     * time1
     * time2
     * @return
     */
    public static  boolean resultYesOrNo(LocalDateTime time1,LocalDateTime time2){

        long diff1 = ChronoUnit.SECONDS.between(time1, time2);
        if(diff1>0){
            return true;
        }
        return false;
      //  return h+"小时"+f+"分钟"+s+"秒";
    }

    /**
      计算一个时间取值多少秒后的新的时间
     * @return
     */
    public static  LocalDateTime withPlus(long time1){
        LocalDateTime localDateTime1 = LocalDateTime.now().plusSeconds(time1).withNano(0);

        return localDateTime1;
    }

    /**
      date转localDateTime
     * @return
     */
    public static  LocalDateTime dateToDateTime(Date time1){
        Instant instant = time1.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;

    }

}
